//package com.blutetoothlibrary;
//
//import android.bluetooth.BluetoothAdapter;
//import android.bluetooth.BluetoothDevice;
//import android.bluetooth.BluetoothManager;
//import android.bluetooth.BluetoothSocket;
//import android.content.BroadcastReceiver;
//import android.content.Context;
//import android.content.Intent;
//import android.content.IntentFilter;
//import android.util.Log;
//
//import com.utils.LogUtils;
//
//import java.io.IOException;
//import java.lang.reflect.Method;
//
///**
// * Created by Administrator on 2017/6/6.
// */
//
//public class BtLibrary {
//    private Context mContext;
//    private BluetoothManager mBluetoothManager;
//    //蓝牙打印机程序
//    private BluetoothAdapter mBluetoothAdapter;
//    private BluetoothManager bluetoothManager;
//    private BluetoothDevice mdevice;
//    private BluetoothSocket mBluetoothSocket;
//    private boolean isFindDevice = false;
//    private int blueConnectCount = 0;
//    //连接打印机
//    private int faildCount = 0;
//    private boolean isFaild = false;
//    private String  mBltMac;
//    private BtLibrary() {
//    }
//
//    public static BtLibrary getInstance() {
//        return BtLibrary.BtLibraryHelp.instance;
//    }
//    static class BtLibraryHelp {
//        static final BtLibrary instance = new BtLibrary();
//    }
//    /**
//     * 蓝牙的初始化
//     *
//     * @param c
//     * @return 蓝牙是否可用
//     */
//    public boolean initialize(Context c) {
//        this.mContext = c;
//        if (mBluetoothManager == null) {
//            mBluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
//            if (mBluetoothManager == null) {
//                return false;
//            }
//        }
//        mBluetoothAdapter = mBluetoothManager.getAdapter();
//        if (mBluetoothAdapter == null) {
//            return false;
//        }
//        mIntentFilter(c);
//        return true;
//    }
//    //注册广播
//    public void mIntentFilter(Context c) {
//        // 用BroadcastReceiver来取得搜索结果
//        IntentFilter intent = new IntentFilter();
//        intent.addAction(BluetoothDevice.ACTION_FOUND);//搜索发现设备
//        intent.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);//设备已连接
//        intent.addAction(BluetoothDevice.ACTION_PAIRING_REQUEST);//配对请求
//        intent.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);//状态改变
//        intent.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);//状态改变
//        intent.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);//行动扫描模式改变了
//        intent.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);//动作状态发生了变化
//        intent.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);//搜索结束
//        c.registerReceiver(mBroadcastReceiver, intent);
//    }
//    /**
//     * 注册广播来接收蓝牙配对信息
//     *
//     * @param context
//     */
//    public BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            String action = intent.getAction();
//            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
//                mdevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//                if (mdevice != null && mdevice.getName() != null && mdevice.getName().contains("BM9000")) {
//                    if (mBluetoothAdapter != null) {
//                        mBluetoothAdapter.cancelDiscovery();
//                    }
//                    isFindDevice = true;
//                    if (mdevice.getBondState() == mdevice.BOND_NONE) {
//                        try {
//                            //通过工具类ClsUtils,调用createBond方法
//                            ClsUtil.createBond(mdevice.getClass(), mdevice);
//                        } catch (Exception e) {
//                            // TODO Auto-generated catch block
//                            e.printStackTrace();
//                        }
//                    } else if (mdevice.getBondState() == mdevice.BOND_BONDED) {
//                        connectBlt(mBltMac);
//                    }
//                }
//            } else if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)) {
//                mdevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//                try {
//                    //1.确认配对
//                    ClsUtil.setPairingConfirmation(mdevice.getClass(), mdevice, true);
//                    //2.终止有序广播
//                    Log.i("order...", "isOrderedBroadcast:" + isOrderedBroadcast() + ",isInitialStickyBroadcast:" + isInitialStickyBroadcast());
//                    abortBroadcast();//如果没有将广播终止，则会出现一个一闪而过的配对框。
//                    //3.调用setPin方法进行配对...
//                    boolean ret = ClsUtil.setPin(mdevice.getClass(), mdevice, "0000");
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//            //状态改变时
//            else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
//                mdevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//                switch (mdevice.getBondState()) {
//                    case BluetoothDevice.BOND_BONDING://正在配对
//                       LogUtils.e("BlueToothTestActivity 正在配对......");
//                        break;
//                    case BluetoothDevice.BOND_BONDED://配对结束
//                       LogUtils.e("BlueToothTestActivity 完成配对");
//                        connectBlt(mBltMac);
//                        break;
//                    case BluetoothDevice.BOND_NONE://取消配对/未配对
//                       LogUtils.e("BlueToothTestActivity 取消配对");
//                    default:
//                        break;
//                }
//            }
//            //搜索完毕广播
//            else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
//                LogUtils.e("bluetoothFinished 搜索完毕");
//                if (!isFindDevice) {
//                    if (mBluetoothAdapter != null) {
//                        if (!isFaild && blueConnectCount > 3) {
////                            loadingDialog.dismiss();
//                            LogUtils.e("没有发现设备");
//                        } else {
//                            new Thread(new Runnable() {
//                                @Override
//                                public void run() {
//                                    mBluetoothAdapter.startDiscovery();
//                                    blueConnectCount++;
//                                }
//                            }).start();
//                        }
//                    }
//                }
//            }
//            //打印机断开
//            else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
//                LogUtils.e("bleDisconnected 连接断开");
//                ConnectDeviceInfo.setBluetoothSocket(null);
//                ConnectDeviceInfo.setIsConnected(false);
//                LogUtils.e("打印机未连接");
//                isFindDevice = false;
//
//            }
//            //打印机连接成功
//            else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
//                LogUtils.e("connected 已连接");
//            }
//            //连接状态改变
//            else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
//                LogUtils.e("change 状态改变：");
//                if (mBluetoothAdapter != null) {
//                    switch (mBluetoothAdapter.getState()) {
//                        case BluetoothAdapter.STATE_TURNING_ON:
//                            LogUtils.e("STATE_TURNING_ON onReceive---------STATE_TURNING_ON");
//                            break;
//                        case BluetoothAdapter.STATE_ON:
//                            LogUtils.e("STATE_ON onReceive---------STATE_ON");
//                            if (faildCount <= 2) {
//                                if (mBluetoothAdapter != null) {
//                                    mBluetoothAdapter.startDiscovery();
//                                }
//                            } else {
//                                faildCount = 0;
//                                isFaild = false;
//                            }
//                            break;
//                        case BluetoothAdapter.STATE_TURNING_OFF:
//                            LogUtils.e("STATE_TURNING_OFF onReceive---------STATE_TURNING_OFF");
//                            break;
//                        case BluetoothAdapter.STATE_OFF:
//                            LogUtils.e("STATE_OFF onReceive---------STATE_OFF");
//                            mBluetoothAdapter.enable();
//                            break;
//                    }
//                }
//            }
//
//        }
//    };
//    public void connectBlt(String  macAddress) {
//        mBltMac=macAddress;
//        try {
//            Class<?> clazz = mBluetoothAdapter.getRemoteDevice(macAddress).getClass();
//            Class<?>[] paramTypes = new Class<?>[]{Integer.TYPE};
//
//            Method m = clazz.getMethod("createRfcommSocket", paramTypes);
//            Object[] params = new Object[]{Integer.valueOf(1)};
//            mBluetoothSocket = (BluetoothSocket) m.invoke(mBluetoothAdapter.getRemoteDevice(mdevice.getAddress()), params);
//
//           LogUtils.e("blueTooth 开始连接...");
//            //在建立之前调用
//            if (mBluetoothAdapter.isDiscovering())
//                //停止搜索
//                mBluetoothAdapter.cancelDiscovery();
//            //如果当前socket处于非连接状态则调用连接
//            if (!mBluetoothSocket.isConnected()) {
//                //你应当确保在调用connect()时设备没有执行搜索设备的操作。
//                // 如果搜索设备也在同时进行，那么将会显著地降低连接速率，并很大程度上会连接失败。
//                try {
//                    mBluetoothSocket.connect();
//                    ConnectDeviceInfo.setBluetoothSocket(mBluetoothSocket);
//                    ConnectDeviceInfo.setIsConnected(true);
////                    tv_right.setText("打印机已连接");
////                    print();
//
//                } catch (IOException e) {
//                    e.printStackTrace();
//
//                    try {
//                        if (mBluetoothSocket != null) {
//                            mBluetoothSocket.close();
//                        }
//                    } catch (IOException ee) {
//                        ee.printStackTrace();
//                    }
//                    if (mBluetoothAdapter != null) {
//                        mBluetoothAdapter.disable();
//                    }
//                    faildCount++;
//                }
//            }
//            LogUtils.e("bleConnected 连接成功");
//        } catch (Exception e) {
//            LogUtils.e("错误："+e.getMessage());
//            LogUtils.e("bleDisconnected 连接失败");
//        }
//    }
//}
